package com.jian.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Provider;
import com.jian.bus.service.GoodsService;
import com.jian.bus.service.ProviderService;
import com.jian.bus.vo.GoodsVo;
import com.jian.sys.common.AppFileUtils;
import com.jian.sys.common.Constant;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.jian.sys.common.Constant.DEFAULT_IMAGE;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-20
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    ProviderService providerService;

    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllGoods")
    public DataGridView loadAllGoods(GoodsVo goodsVo) {

        IPage<Goods> page = new Page<>(goodsVo.getPage(), goodsVo.getLimit());
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.like(goodsVo.getProviderid() != null && goodsVo.getProviderid() != 0, "providerid", goodsVo.getProviderid());
        wrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()), "goodsname", goodsVo.getGoodsname());
        wrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()), "productcode", goodsVo.getProductcode());
        wrapper.like(StringUtils.isNotBlank(goodsVo.getPromitcode()), "promitcode", goodsVo.getPromitcode());
        wrapper.like(StringUtils.isNotBlank(goodsVo.getDescription()), "description", goodsVo.getDescription());
        wrapper.like(StringUtils.isNotBlank(goodsVo.getSize()), "size", goodsVo.getSize());
        this.goodsService.page(page, wrapper);
        List<Goods> goodsList = page.getRecords();
        for (Goods goods : goodsList) {
            Provider provider = this.providerService.getById(goods.getProviderid());
            goods.setProvidername(provider.getProvidername());
        }
        return new DataGridView(page.getTotal(), goodsList);
    }

    /**
     * 添加商品
     *
     * @param goodsVo
     * @return
     */
    @RequestMapping("/addGoods")
    public ResultObj addGoods(GoodsVo goodsVo) {
        try {
            //判断是否为临时图片图片
            if (goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().endsWith("_temp")) {
                String newFile = AppFileUtils.renameFile(goodsVo.getGoodsimg());
//                System.out.println("newFile---》" + newFile);
                goodsVo.setGoodsimg(newFile);
            }
            this.goodsService.save(goodsVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改商品
     *
     * @param goodsVo
     * @return
     */
    @RequestMapping("/updateGoods")
    public ResultObj updateGoods(GoodsVo goodsVo) {
        try {
            if (goodsVo.getGoodsimg() != null && !goodsVo.getGoodsimg().equals(DEFAULT_IMAGE)) {
                if (goodsVo.getGoodsimg().endsWith("_temp")) {
                    String newFIle = AppFileUtils.renameFile(goodsVo.getGoodsimg());
                    goodsVo.setGoodsimg(newFIle);
                    //删除原先的图片
                    String oldPath = this.goodsService.getById(goodsVo.getId()).getGoodsimg();
                    AppFileUtils.removeFileByPath(oldPath);
                }
            }
            this.goodsService.updateById(goodsVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除商品
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteGoods")
    public ResultObj deleteGoods(Integer id) {
        try {
            //删除对应的图片文件
            AppFileUtils.removeFileByPath(goodsService.getById(id).getGoodsimg());
            this.goodsService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 查询所有可用的商品信息
     */
    @RequestMapping("/loadAllGoodsName")
    public DataGridView loadAllGoodsName(){
        QueryWrapper<Goods> wrapper = new QueryWrapper();
        wrapper.eq("available",Constant.AVAILABLE_TRUE);
        List<Goods> list = this.goodsService.list(wrapper);
        for (Goods goods : list) {
            goods.setProvidername(providerService.getById(goods.getProviderid()).getProvidername());
        }
        return new DataGridView(list);
    }
    /**
     * 查询所有可用的商品信息通过供应商
     */
    @RequestMapping("/loadAllGoodsNameByProviderId")
    public DataGridView loadAllGoodsNameByProviderId(Integer providerid){
        QueryWrapper<Goods> wrapper = new QueryWrapper();
        wrapper.eq("available",Constant.AVAILABLE_TRUE);
        wrapper.eq("providerid",providerid);
        List<Goods> list = this.goodsService.list(wrapper);
        for (Goods goods : list) {
            goods.setProvidername(providerService.getById(goods.getProviderid()).getProvidername());
        }
        return new DataGridView(list);
    }
}

